using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Batch.Dto;
using JTZY.Model.Batch;
using JTZY.Repository;
using JTZY.Service.Batch.IBatchService;
using JTZY.Model.Order;
using Essensoft.Paylink.WeChatPay.V3.Domain;

namespace JTZY.Service.Batch
{
    /// <summary>
    /// 批量发货明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IBatchshipitemService), ServiceLifetime = LifeTime.Transient)]
    public class BatchshipitemService : BaseService<Batchshipitem>, IBatchshipitemService
    {
        /// <summary>
        /// 查询批量发货明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<Batchshipitem> GetList(BatchshipitemQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToList();

            var details=Context.Queryable<Batchshipdetail>()
                .LeftJoin<Corecmsbillaftersales>((detail, after) => detail.AftersalesId == after.AftersalesId)
                .LeftJoin<Batchship>((detail, after,ship) => detail.OrderId == after.OrderId)
                .Where((detail, after, ship) => detail.OrderId == parm.OrderId)
                .Select((detail, after, ship) =>new Batchshipdetail { 
                    SnCode=detail.SnCode,
                    AftersalesId=detail.AftersalesId,
                    ApplyTime=after.CreateTime,
                    Status=ship.Status,
                    ProductId=detail.ProductId,
                }).ToList();

            response.ForEach(item => {
                var list = details.Where(p => p.ProductId == item.ProductId).ToList();
                item.details = list;
            });

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Batchshipitem GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加批量发货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Batchshipitem AddBatchshipitem(Batchshipitem model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改批量发货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBatchshipitem(Batchshipitem model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Batchshipitem> QueryExp(BatchshipitemQueryDto parm)
        {
            var predicate = Expressionable.Create<Batchshipitem>();
            predicate = predicate.And(it => it.OrderId == parm.OrderId); 
            return predicate;
        }
    }
}